#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Services;
using Library.Entity;

#endregion

namespace Library
{
    public partial class UserControlLogin : UserControl
    {
        UserService m_userService;
        User m_user;
        MainForm m_mainForm;

        public UserControlLogin(MainForm mainForm)
        {
            InitializeComponent();
            m_mainForm = mainForm;
            m_userService = new UserService();
            textBoxMessage.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (isValidUser())
            {
                this.Hide();
                //m_user.Role = m_userService.GetRoleMasterByID(m_user.Role.RoleMasterID);
                //m_userService.GetUserRoleMasterByUserID(m_user);
                m_mainForm.User = m_user;
                insertloginSession();
                m_mainForm.ShowMainFormPanel(true);
                textBoxMessage.Visible = false;
            }
            clearForm();
            textBoxUserCode.SelectAll();
        }


        private void insertloginSession()
        {
            m_user.SESSION_ID = this.Session.SessionID;
            m_userService.InsertLog(m_user, "Log IN");
        }

        public void InsertlogoutSession()
        {
            m_userService.InsertLog(m_user, "Log OUT");
        }

        private void clearForm()
        {
            textBoxPassword.Text = "";
            textBoxUserCode.Text = "";
            textBoxUserCode.Focus();
        }

        public void SetMessage(string text)
        {
            textBoxMessage.Text = text;
            textBoxMessage.Visible = true;
        }

        private bool isValidUser()
        {
            m_user = m_userService.GetUserByUserCode(textBoxUserCode.Text);
            if (m_user == null)
            {
                SetMessage("The user code or password you entered isn't correct. Try entering it again.");
                return false;
            }
            if (!m_user.Active)
            {
                SetMessage("The user code you entered is inactive.");
                return false;
            }
            if (textBoxPassword.Text != m_user.Password)
            {
                SetMessage("The user code or password you entered isn't correct. Try entering it again.");
                return false;
            }
            return textBoxPassword.Text == m_user.Password;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            clearForm();
            textBoxMessage.Text = "";
        }

        private void textBoxUserCode_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            textBoxPassword.Focus();
            textBoxPassword.SelectAll();
        }

        private void textBoxPassword_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            buttonLogin.Select();
            buttonLogin.Focus();
        }


    }

}